﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("merchIntroduce")]
    public class MerchIntroduceController : AdminController
    {
        private IMerchIntroduceService _iMerchIntroduceService;

        public MerchIntroduceController(IMerchIntroduceService iMerchIntroduceService)
        {
            _iMerchIntroduceService = iMerchIntroduceService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(MerchIntroduceQuery query)
        {
            var model = _iMerchIntroduceService.GetMerchIntroduces(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            merch_introduce_dto_ex model = new merch_introduce_dto_ex() { status = EMerchIntroduceStatus.Normal, is_recommend = false, logo_url = "" };
            if (id > 0)
                _iMerchIntroduceService.GetMerchIntroduce(id).ToCopyProperty(model);
            model.status_value = ((int)model.status).ToString();//ng-select 需要绑定字符串类型
            model.is_recommend_value = (model.is_recommend ? "1" : "0");//ng-select 需要绑定字符串类型
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(merch_introduce_dto_ex dto)
        {
            dto.status = (EMerchIntroduceStatus)int.Parse(dto.status_value);
            dto.is_recommend = dto.is_recommend_value == "true" ? true : false;
            var file_url = Upload("merchIntroduce");
            if (!string.IsNullOrEmpty(file_url))
                dto.logo_url = file_url;
            else
                return Json(ResultToJson.Json(1001, "请先上传图片", new { }), JsonRequestBehavior.AllowGet);

            var result = _iMerchIntroduceService.AddMerchIntroduce(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(merch_introduce_dto_ex dto)
        {
            dto.status = (EMerchIntroduceStatus)int.Parse(dto.status_value);
            dto.is_recommend = dto.is_recommend_value == "1" ? true : false;
            var file_url = Upload("merchIntroduce");
            if (!string.IsNullOrEmpty(file_url))
                dto.logo_url = file_url;

            var result = _iMerchIntroduceService.UpdateMerchIntroduce(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpDelete]
        public JsonResult Delete(long id)
        {
            var result = _iMerchIntroduceService.DeleteMerchIntroduce(id);
            return Json(ResultToJson.Json(result > 0 ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status)
        {
            var result = _iMerchIntroduceService.UpdateMerchIntroduceStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }

        [HttpPost]
        public JsonResult UpdateIsRecommend(long id, int isRecommend)
        {
            bool isRecommendValue = isRecommend == 1 ? true : false;
            var result = _iMerchIntroduceService.UpdateMerchIntroduceIsRecommend(id, isRecommendValue);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }
    }
}